package com.mfangsoft.zhuangjialong.common.utils;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.imageio.ImageIO;

import com.mfangsoft.zhuangjialong.common.controller.FileUploadController;

public class StreamUtils {

    public static InputStream getStringStream(String sInputString){  
        
     if(sInputString != null && !sInputString.trim().equals("")){  
    	 return new ByteArrayInputStream(sInputString.getBytes());
     }
     return null;
    }
    /**
     * 将二进制转换成文件保存
     * @param instreams 二进制流
     * @param imgPath 图片的保存路径
     * @param imgName 图片的名称
     * @return 文件访问地址
     */
    public static String saveToImgByInputStream(String fileName,InputStream instreams,String logoUrl,boolean ishyaline){
    	
    	String imgurl = null;
    	java.text.SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
    	String format = f.format(new Date());
    	String path = PropUtis.getValue(PropUtis.FILE_PATH) + "/QR/" +format ;
		System.out.println("path--------------------" + path);
		File filepath = new File(path);
		if (!filepath.exists()) {

			filepath.mkdirs();
		}
		Long time =System.currentTimeMillis();
        if(instreams != null){
            try {
            	BufferedImage qr = ImageIO.read(instreams);
  
            	if(logoUrl!=null && !logoUrl.equals("")){
            	
            		//获取logo图片
            	BufferedImage logo =  ImgUtil.getUrlByBufferedImage(logoUrl);
            	 
            	//处理图片将其压缩成正方形的小图  
	            BufferedImage  convertImage= ImgUtil.scaleByPercentage(logo, 180,180);  
	           
	            //裁剪成圆形 （传入的图像必须是正方形的 才会 圆形 如果是长方形的比例则会变成椭圆的）  
	            convertImage = ImgUtil.convertCircular(convertImage);  
	            
	            
	            ImgUtil.insertImage(qr, convertImage, true);
            	}
            	//将二维码透明化
            	if(ishyaline){
            		qr = ImgUtil.hyalinize(qr);
            	}
            	ByteArrayOutputStream out = new ByteArrayOutputStream();
            	ImageIO.write(qr, "png", out);
            	if(fileName!=null){
            		FileUploadController.httpClientUploadFileBase64(out.toByteArray(), path, fileName + ".png");
            		//ImageIO.write(qr, "png", new File(filepath.getPath() + "/" + fileName + ".png"));  
            		imgurl=PropUtis.getValue(PropUtis.FILE_URL) + "/QR/" + format + "/" + fileName + ".png";
            	}else{
            		//ImageIO.write(qr, "png", new File(filepath.getPath() + "/" + time + ".png"));  
            		FileUploadController.httpClientUploadFileBase64(out.toByteArray(), path, time + ".png");
            		imgurl=PropUtis.getValue(PropUtis.FILE_URL) + "/QR/" + format + "/" + time + ".png";
            	}
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
            }
        }
        return imgurl;
    }
    
/*    public static String saveToImgByInputStream(String fileName,InputStream instreams){
    	String imgurl = null;
    	java.text.SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
    	String path = PropUtis.getValue(PropUtis.FILE_PATH) + "/QR/" + f.format(new Date());
    	System.out.println("path--------------------" + path);
    	File filepath = new File(path);
    	if (!filepath.exists()) {
    		
    		filepath.mkdirs();
    	}
    	Long time =System.currentTimeMillis();
    	if(instreams != null){
    		try {
    			File file ;
    			if(fileName!=null){
    				file = new File(filepath.getPath() + "/" + fileName + ".jpg");
    			}else{
    				file = new File(filepath.getPath() + "/" + time + ".jpg");
    			}
    			FileOutputStream fos=new FileOutputStream(file);
    			byte[] b = new byte[1024];
    			int nRead = 0;
    			while ((nRead = instreams.read(b)) != -1) {
    				fos.write(b, 0, nRead);
    			}
    			fos.flush();
    			fos.close();  
    			imgurl=PropUtis.getValue(PropUtis.FILE_URL) + "/QR/" + f.format(new Date()) + "/" + time + ".jpg";
    		} catch (Exception e) {
    			e.printStackTrace();
    		} finally {
    		}
    	}
    	return imgurl;
    }
    
*/}
